﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OAonline.Models;
using System.IO;
using System.Xml.Linq;
using CommonLibrary.DataModel.Form;
using ServiceApplication.Service;
using CommonLibrary.Common;
using ServiceApplication.Service.Basic;
using System.Xml;

namespace OAonline.Controllers
{
    public class FormController : Controller
    {
        FormService formService = new FormService();
        SerializeDesigner designer = new SerializeDesigner();
        BasicService basicService = new BasicService();

        public ActionResult Index(UserModel user)
        {
            FormModel formModel = new FormModel
            {
                dt_forms = formService.QueryFormDataTable(user.user.id)
            };

            return View(formModel);
        }

        //显示表单
        public ActionResult FormShow(int form_id)
        {
            string formdefine = string.Empty;
            FormEntity formEntity = formService.QueryFormEntity(form_id);
            if (formEntity != null)
            {
                XElement xml = XElement.Load(Server.MapPath("~/UploadFiles/Form/" + formEntity.location));

                //返回
                formdefine = designer.DeserializeXmlToWeb(xml);
            }
            FormModel fm = new FormModel
            {
                dt_forms = null,
                formdefi = formdefine,
            };
            return View(fm);
        }


        #region 调用方法
        //上传表单文件
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Import(HttpPostedFileBase fileData, UserModel user)
        {
            string result = string.Empty;
            if (null != fileData)
            {
                try
                {
                    //保存到文件
                    string filename = Path.GetFileName(fileData.FileName);//获得文件名
                    string ext = Path.GetExtension(fileData.FileName);//获得文件扩展名
                    if (!ext.Equals(".rar", StringComparison.CurrentCultureIgnoreCase))
                    {
                        return JavaScript("alter('请上传压缩包类型文件');");
                    }

                    string filepath = Server.MapPath("~/UploadFiles/Form/");
                    FormEntity formEntity = null;
                    if (!Directory.Exists(filepath))
                    {
                        Directory.CreateDirectory(filepath);
                    }
                    String fileNameWithoutExten = Path.GetFileNameWithoutExtension(filename);
                    Boolean dirExist;
                    dirExist = Directory.Exists(Path.Combine(filepath, fileNameWithoutExten));
                    //FileInfo f = new FileInfo(filepath + filename);

                    ZipHelper zipHelper = new ZipHelper();
                    //如果存在，则自动更新文件
                    if (dirExist)
                    {
                        //f.Delete();
                        Directory.Delete(Path.Combine(filepath, fileNameWithoutExten), true);
                        fileData.SaveAs(Path.Combine(filepath, filename));

                        // 解压压缩包
                        zipHelper.DecompressFile(Path.Combine(filepath, filename));

                        XElement xml = XElement.Load(Path.Combine(filepath + fileNameWithoutExten, fileNameWithoutExten + ".xml"));
                        result = designer.DeserializeXmlToWeb(xml);
                    }
                    else
                    {
                        fileData.SaveAs(Path.Combine(filepath, filename));

                        // 解压压缩包
                        zipHelper.DecompressFile(Path.Combine(filepath, filename));

                        XElement xml = XElement.Load(Path.Combine(filepath + fileNameWithoutExten, fileNameWithoutExten + ".xml"));

                        //保存到数据库
                        result = designer.DeserializeXmlToWeb(xml);
                        formEntity = designer.DeserializeXmlToFormEntity(xml);
                        formEntity.user_name = user.user.name;
                        formEntity.catalogy = "";
                        formEntity.createtime = DateTime.Now;
                        formEntity.dept_id = -1;
                        formEntity.descn = "";
                        formEntity.lasttime = DateTime.Now;
                        formEntity.location = fileNameWithoutExten + "/" + fileNameWithoutExten + ".xml";
                        formEntity.user_id = user.user.id;
                        formEntity = formService.AddForm(formEntity);
                    }
                }
                catch (Exception e)
                {
                    return JavaScript("alter('" + e.Message + "');");
                }
            }
            return RedirectToAction("Index");
        }

        public String QueryTable(String txtValue, String sqlValue)
        {
            String table = String.Empty;
            if (txtValue != String.Empty)
            {
                txtValue = txtValue.Replace("\n", "");
                var strs =CommonMethod.splitString(txtValue);
                if (strs.Count() == 0)
                    return "wrong";
                table = designer.GetTableWithData(strs, sqlValue);

            }
            //else
            //    table = designer.GetTableWithData(sqlValue);
            return table;
        }

        #endregion


    }
}
